package com.xzx.mr.order2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 获取每个订单中最大的金额，订单号升序排列
 * 方案一：使用原始的方案，在reduce阶段比较输出金额最大的订单
 * 方案二：使用groupingComparator，在reduce阶段进行处理
 *         将订单号和价格作为key处理，在reduce之前，就选择出，订单中最大的那个价格
 *          订单id相同就认为是同一个key，分到同一组，然后再reduce中取第一个就行
 *         见order2包
 *
 * @author xinzhixuan
 * @version V1.0
 * @date 2019/7/21 21:59
 */
public class OrderDriver2 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        args = new String[]{"D:\\hadoopinput\\orderinput\\order.txt", "d:\\output4"};

        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        job.setJarByClass(OrderDriver2.class);

        job.setMapperClass(OrderMapper.class);
        job.setReducerClass(OrderReducer.class);

        job.setMapOutputKeyClass(OrderBean.class);
        job.setMapOutputValueClass(NullWritable.class);

        job.setOutputKeyClass(OrderBean.class);
        job.setOutputValueClass(NullWritable.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.setGroupingComparatorClass(OrderSortGroupingComparator.class);

        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}
